MonolayerCultures / src / AllCells / [RC17]ClusterTree.Rmd
[RC17]ClusterTree.Rmd
Raw
---
title: '[RC17]ClusterTree'
author: "Nina-Lydia Kazakou"
date: "14/03/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Set-up

### Load libraries

```{r message=FALSE, warning=FALSE}
library(SingleCellExperiment)
library(Seurat)
library(tidyverse)
library(Matrix)
library(scales)
library(cowplot)
library(RCurl)
library(scDblFinder)
library(Matrix)
library(ggplot2)
library(edgeR)
library(dplyr)
library(ggsci)  
library(here)
library(gtools)
```

### Colour Palette

```{r load_palette}
mypal <- pal_npg("nrc", alpha = 0.7)(10)
mypal2 <-pal_tron("legacy", alpha = 0.7)(7)
mypal3<-pal_lancet("lanonc", alpha = 0.7)(9)
mypal4<-pal_simpsons(palette = c("springfield"), alpha = 0.7)(16)
mypal5 <- pal_rickandmorty(palette = c("schwifty"), alpha = 0.7)(6)
mypal6 <- pal_futurama(palette = c("planetexpress"), alpha = 0.7)(5)
mypal7 <- pal_startrek(palette = c("uniform"), alpha = 0.7)(5)
mycoloursP<- c(mypal, mypal2, mypal3, mypal4)
```

### Load object
```{r}
RC17_norm <- readRDS(here("data", "Processed", "AllCells", "RC17_AllCells_srt.rds"))
```

```{r}
RC17_norm <- FindNeighbors(RC17_norm, dims = 1:18, verbose = FALSE)
RC17_norm <- FindClusters(RC17_norm, resolution = c(0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), verbose = FALSE)
```

```{r}
RC17_norm <- RunUMAP(RC17_norm, dims = 1:18)
```


# Generate & Save DimPlots for all the calculated resolutions:
```{r eval=FALSE}
plot_list_func <- function(seur_obj,
                           col_pattern, 
                           plot_cols, 
                           clust_lab = TRUE,
                           label_size = 8,
                           num_col = 4,
                           save_dir = getwd(),
                           width=7,
                           height=5){
  extr_res_col <- grep(pattern = col_pattern, names(seur_obj@meta.data))
  res_names <- names(seur_obj@meta.data[extr_res_col])
  # gtools function, sorts gene_names alphanumeric:
  res_names <- mixedsort(res_names) 
  plot_l <-list()
  for(i in 1: length(res_names)){
    pdf(paste0(save_dir, 
               res_names[i], "_umap.pdf"), width=width, height=height)
    dim_plot <- DimPlot(seur_obj, 
                        reduction = "umap", 
                        cols= plot_cols,
                        group.by = res_names[i],
                        label = clust_lab,
                        label.size = label_size) + NoLegend()
    print(dim_plot)
    dev.off()
    print(dim_plot)
  }
}


dir.create(here("outs", "Processed", "AllCells", "DimPlots_AllRes"))

plot_list_func(seur_obj = RC17_norm,
               col_pattern = "RNA_snn_res.",
               plot_cols = mycoloursP[6:40],
               save_dir = here("outs", "Processed", "AllCells", "DimPlots_AllRes"))
```



# ClusterTree
ttps://cran.r-project.org/web/packages/clustree/vignettes/clustree.html

A clustering tree is different in that it visualises the relationships between at a range of resolutions.
To build a clustering tree I need to look at how cells move as the clustering resolution is increased. Each cluster forms a node in the tree and edges are constructed by considering the cells in a cluster at a lower resolution (say k=2) that end up in a cluster at the next highest resolution (say k=3). By connecting clusters in this way we can see how clusters are related to each other, which are clearly distinct and which are unstable.

```{r message=FALSE, warning=FALSE}
library(clustree)
```

```{r}
combined_clust_res <- cbind(k.0.1 = RC17_norm@meta.data$RNA_snn_res.0.01,
                            k.0.5 = RC17_norm@meta.data$RNA_snn_res.0.05,
                            k.1 = RC17_norm@meta.data$RNA_snn_res.0.1,
                            k.2 = RC17_norm@meta.data$RNA_snn_res.0.2,
                            k.3 = RC17_norm@meta.data$RNA_snn_res.0.3,
                            k.4 = RC17_norm@meta.data$RNA_snn_res.0.4, 
                            k.5 = RC17_norm@meta.data$RNA_snn_res.0.5, 
                            k.6 = RC17_norm@meta.data$RNA_snn_res.0.6,
                            k.7 = RC17_norm@meta.data$RNA_snn_res.0.7,
                            k.8 = RC17_norm@meta.data$RNA_snn_res.0.8,
                            k.9 = RC17_norm@meta.data$RNA_snn_res.0.9,
                            k.10 = RC17_norm@meta.data$RNA_snn_res.1)
```

```{r fig.height=10, fig.width=12, message=FALSE, warning=FALSE}
clustree(combined_clust_res, prefix="k.", edge_arrow=FALSE)
```

```{r eval=FALSE}
dir.create(here("outs", "Processed", "AllCells", "ClusterTree"))

clust_tree_01 <- clustree(combined_clust_res, prefix="k.", edge_arrow=FALSE)

# Step 1: Call the pdf command to start the plot
pdf(file = here("outs", "Processed", "AllCells", "ClusterTree", "ClusterTree_01.pdf"),
    width = 10,
    height = 10)
# Step 2: Create the plot with R code
print(clust_tree_01)
# Step 3: Run dev.off() to create the file!
dev.off()
```

In this plot, the size of each node is related to the number of samples in each cluster and the color indicates the clustering resolution.
The edges are coloured according to the number of samples they represent and the transparency shows the incoming node proportion, the number of samples in the edge divided by the number of samples in the node it points to.

```{r clustree, fig.height=10, fig.width=10}
clustree(
  RC17_norm,
  prefix = paste0("RNA", "_snn_res."),
  exprs = c("data", "counts", "scale.data"),
  assay = NULL
)
```

```{r eval=FALSE}
clust_tree_02 <- clustree(
  RC17_norm,
  prefix = paste0("RNA", "_snn_res."),
  exprs = c("data", "counts", "scale.data"),
  assay = NULL
)

pdf(file = here("outs", "Processed", "AllCells", "ClusterTree", "ClusterTree_02.pdf"),
    width = 10,
    height = 10)
print(clust_tree_01)
dev.off()
```

```{r}
RC17_norm@meta.data$RNA_snn_res.0.9 <- NULL
RC17_norm@meta.data$RNA_snn_res.1 <- NULL
```

```{r clustree_0.8, fig.height=10, fig.width=10}
clustree(
  RC17_norm,
  prefix = paste0("RNA", "_snn_res."),
  exprs = c("data", "counts", "scale.data"),
  assay = NULL
)
```

```{r eval=FALSE}
clust_tree_02 <- clustree(
  RC17_norm,
  prefix = paste0("RNA", "_snn_res."),
  exprs = c("data", "counts", "scale.data"),
  assay = NULL
)

pdf(file = here("outs", "Processed", "AllCells", "ClusterTree", "ClusterTree_03.pdf"),
    width = 10,
    height = 10)
print(clust_tree_01)
dev.off()
```


```{r}
sessionInfo()
```